<template>
    <div>
        <h1>居民管理显示</h1>
        姓名 <input type="text" v-model="info.names">
        类型
        <select v-model="info.lx">
            <option value="">请选择</option>
            <option value="业主">业主</option>
            <option value="租户">租户</option>
        </select>
        <input type="button" value="查询" @click="getlist">
        <input type="button" value="批量删除" @click="del">
        <input type="button" value="批量审核" @click="getxg(true)">

        <input type="button" value="导出" @click="fn">
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td><input type="checkbox" v-model="checked" @click="checkedall"></td>
                    <td>Id</td>
                    <td>头像</td>
                    <td>房间</td>
                    <td>姓名</td>
                    <td>性别</td>
                    <td>民族</td>
                    <td>籍贯</td>
                    <td>证件</td>
                    <td>电话</td>
                    <td>类型</td>
                    <td>楼栋</td>
                    <td>审核状态</td>
                    <td>操作</td>
                </tr>

                <tr v-for="itme in data.list" :key="itme.Id">
                    <td><input type="checkbox" v-model="ids" :value="itme.Id" @change="checkedone"></td>
                    <td>{{ itme.Id }}</td>
                    <td><img :src="itme.Img" width="30" height="30"></td>
                    <td>{{ itme.FJ }}</td>
                    <td>{{ itme.Name }}</td>
                    <td>{{ itme.Sex }}</td>
                    <td>{{ itme.MZ }}</td>
                    <td>{{ itme.JG }}</td>
                    <td>{{ itme.SFZH }}</td>
                    <td>{{ itme.Tel }}</td>
                    <td>{{ itme.LX }}</td>
                    <td>{{ itme.Guanli }}</td>
                    <td>{{ itme.State == true ? "已审核" : "未审核" }}</td>
                    <td><input type="button" value="删除" @click="dels(itme.Id)">
                        <input type="button" value="修改" @click="xg(itme.Id)">
                    </td>
                </tr>
            </tbody>
        </table>
        <span><b>共{{ data.totalcount }}条</b></span>
        <span><b>每页{{ info.pagesize }}条</b></span>
        <span><b>共 {{ info.pageindex }}/{{ data.totalpage }}页</b></span>
        <input type="button" value="首页" @click="gopage(1)">
        <input type="button" value="上一页" @click="gopage(info.pageindex - 1)">
        <span v-for="itme in data.totalpage" @click="gopage(itme)">{{ itme }}</span>
        <input type="button" value="下一页" @click="gopage(info.pageindex + 1)">
        <input type="button" value="尾页" @click="gopage(data.totalpage)">
        <select v-model="info.pagesize" @click="show()">
            <option :value="itme" v-for="itme in fy">
                显示{{ itme }}条
            </option>
        </select>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import axios from 'axios';
import { useRouter } from 'vue-router';
const rouder = useRouter();
let info: any = reactive({
    names: '',
    lx: '',
    pageindex: 1,
    pagesize: 2
})
let data: any = reactive({
    list: [],
    totalcount: 0,
    totalpage: 0
})
let checked: any = ref(false);
let ids: any = ref([]);
let fy: any = ref([2, 4, 10, 15, 20])
onMounted(() => {
    getlist()
})
const getlist = () => {
    axios({
        url: '/api/Wook/GetShou',
        method: 'get',
        params: info
    })
        .then((res) => {
            console.log(res);
            Object.assign(data, res.data)
        })
        .catch((err) => {

        })
}
//===============================分页===========================
const show = () => {
    info.pageindex = 1;
    getlist();
}
const gopage = (num: any) => {
    if (num < 1) {
        info.pageindex = 1;
        return;
    }
    if (num > data.totalpage) {
        info.pageindex = data.totalpage;
        return;
    }
    info.pageindex = num;
    getlist();
}
//=========================================批量 操作===============================
const del = () => {
    axios({
        url: '/api/Wook/DElETE',
        method: 'delete',
        params: {
            id: ids.value.toString()
        }
    })
        .then((res) => {
            console.log(res);
            if (res.data > 0) {
                alert("删除成功");
                getlist();
                return;
            }
            else {
                alert("删除失败");
                return;
            }


        })
}

const getxg = (state: any) => {
    axios({
        url: '/api/Wook/ZTXG',
        method: 'get',
        params: {
            id: ids.value.toString(),
            state: state
        }
    })
        .then((res) => {
            console.log(res);
            if (res.data > 0) {
                alert("修改成功");
                checked.value = false;
                ids.value = [];
                getlist();
                return;
            }
            else {
                alert("修改失败");
                return;
            }


        })
}
const dels = (id: any) => {
    axios({
        url: '/api/Wook/DElETE',
        method: 'delete',
        params: {
            id: id
        }
    })
        .then((res) => {
            console.log(res);
            if (res.data > 0) {
                alert("删除成功");
                getlist();
                return;
            }
            else {
                alert("删除失败");
                return;
            }


        })
}

const xg = (id: any) => {
    rouder.push({
        path: 'xgjm',
        query: { id: id }
    })
}


//===================================全选全不选=======================
const checkedone = () => {
    if (ids.value.length == data.list.length) {
        checked.value = true;
        return;
    }
    else {
        checked.value = false;
        return;
    }
}
const checkedall = () => {
    checked.value = !checked.value
    if (checked.value == true) {
        ids.value = data.list.map((itme: any) => {
            return itme.Id;
        })
    }
    else {
        ids.value = [];
    }
}
//===================================导出==========================
const fn = () => {
    location.href = `http://localhost:5143/api/Wook/EXpor?pageindex=${info.pageindex}&pagesize=${info.pagesize}&names=${info.names}&lx=${info.lx}`
}

</script>

<style scoped></style>